package MicroSpring.beanFactoryPostProcessor.impl;

import MicroSpring.annotation.ComponentScan;
import MicroSpring.beanFactory.impl.DefaultListableBeanFactory;
import MicroSpring.beanFactoryPostProcessor.BeanFactoryPostProcessor;

import java.io.File;

/*
    启动类处理器.
 */
public class BootStrapClassPostProcessor implements BeanFactoryPostProcessor {


    @Override
    public void postProcessBeanDefinitionRegistry(DefaultListableBeanFactory beanFactory) {
        if (beanFactory.isBootStrap()){
            Class bootClass = beanFactory.getBootClass();
            dealWithComponentScan(beanFactory, bootClass);
        }
    }

    /**
     * *处理ComponetScan注解,获取扫描地址.
     * @param beanFactory
     * @param bootClass
     */
    private void dealWithComponentScan(DefaultListableBeanFactory beanFactory, Class bootClass) {
        if (bootClass.isAnnotationPresent(ComponentScan.class)){
            ComponentScan componentScan = (ComponentScan) bootClass.getAnnotation(ComponentScan.class);
            String scap = componentScan.value();
            String p1 = beanFactory.getFINAL_SCAN_PATH();
            scap=scap.replace(".", File.separator);
            p1+=scap;
            beanFactory.setFINAL_SCAN_PATH(p1);
        }
    }
}
